package ui.activity.profit;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.widget.TextView;

import com.jakewharton.rxbinding.view.RxView;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.inject.Inject;

import Utils.SharedPrefStore;
import base.BaseAct;
import butterknife.BindView;
import event.NetErrorEvent;
import ui.activity.profit.biz.SignBiz;
import ui.activity.profit.component.DaggerSignComponent;
import ui.activity.profit.contract.SignContract;
import ui.activity.profit.module.SignModule;
import ui.activity.profit.presenter.SignPresenter;
import ui.model.IntegralBean;
import ui.model.SignBean;
import util.LogUtils;
import util.SysUtils;
import widget.signcalendar.MonthSignData;
import widget.signcalendar.SignCalendar;


/**
 * 登录界面
 * Created by chenjian on 2017/8/4.
 */

public class SignAct extends BaseAct implements SignContract.View {


    @Inject
    SignPresenter presenter;
    @Inject
    SignBiz biz;
    @BindView(R.id.sign_in)
    TextView sign;
    @BindView(R.id.sign_day)
    TextView signDay;
    @BindView(R.id.sign_calendar)
    SignCalendar signCalendar;
    @BindView(R.id.sign_integral)
    TextView signIntegral;
    SharedPrefStore store;
    String customId;

    ArrayList<MonthSignData> monthDates;
    MonthSignData firstMonthData;
    boolean isSign = true;
    MonthSignData monthToday = new MonthSignData();

    int year = 0;
    int month = 0;
    int day = 0;

    @Override
    protected void setRoot() {
        setTitleBar("#FF6969");
        setContentView(R.layout.activity_sign);

    }

    @Override
    protected void setup() {
        DaggerSignComponent.builder().signModule(new SignModule(this)).build().inject(this);
        setToolBar(getResources().getString(R.string.lang_sign), "#FF6969");
        presenter.setBiz(biz);
    }


    @Override
    protected void init(@Nullable Bundle savedInstanceState) {
        store = new SharedPrefStore();
        customId = store.getString("id", "0");
        monthDates = new ArrayList<>();
        Date date = new Date();
        year = date.getYear() + 1900;
        month = date.getMonth() + 1;
        day = date.getDate();
        firstMonthData = new MonthSignData();
        firstMonthData.setYear(year);
        firstMonthData.setMonth(month);
        ArrayList<Date> signDates2 = new ArrayList<>();
        firstMonthData.setSignDates(signDates2);
        monthDates.add(firstMonthData);
        signCalendar.setToday(new Date());
        signCalendar.setSignDatas(monthDates);
        presenter.getIntegralInfo(customId);
        presenter.getSignInfo(customId, year, month);
        RxView.clicks(sign)
                .compose(this.<Void>bindToLifecycle())
                .throttleFirst(500, TimeUnit.MILLISECONDS)
                .subscribe(Void -> {
                    if (isSign) {
                        showMsg("您已经签到成功");
                        return;
                    }
                    presenter.signIn(customId);
                });

        signCalendar.datePager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                signCalendar.pagerIndex = position;
                if (position == 0) {
                    presenter.getSignInfo3(customId, year, month);
                }
                signCalendar.setTitleContent(position, true);
                signCalendar.setArrowStateByPosition(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }

        });


    }


    @Override
    public void onNetErrorEvent(NetErrorEvent event) {
        super.onNetErrorEvent(event);
    }

    @Override
    protected void onNetError(NetErrorEvent event) {

    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        biz.onDestory();
    }


    @Override
    public void upDateUI(SignBean bean, int mYear, int mMonth) {
        signDay.setText("已连续签到" + bean.getContinued() + "天，继续加油！");
//        MonthSignData monthData = new MonthSignData();
        monthToday.setYear(mYear);
        monthToday.setMonth(mMonth - 1);
        ArrayList<Date> signDates = new ArrayList<>();
        List<String> strings = new ArrayList<>();
        if (bean.getDateList() != null && bean.getDateList().size() > 0) {
            for (int i = 0; i < bean.getDateList().size(); i++) {
                Date date = new Date(bean.getDateList().get(i));
                signDates.add(date);
                strings.add(SysUtils.sf1.format(new Date(bean.getDateList().get(i))));
            }
        }
        if (strings.contains(SysUtils.sf1.format(new Date()))) {
            sign.setText("已签到");
            isSign = true;
        } else {
            sign.setText("签到");
            isSign = false;
        }
        monthToday.setSignDates(signDates);
        monthDates.add(0, monthToday);
        signCalendar.setSignDatas(monthDates);
        month = mMonth;
        year = mYear;
        presenter.getSignInfo2(customId, year, month);
    }


    @Override
    public void upDateUI2(SignBean bean, int mYear, int mMonth) {
        month = mMonth;
        year = mYear;
        MonthSignData monthData = new MonthSignData();
        monthData.setYear(mYear);
        monthData.setMonth(mMonth - 1);
        ArrayList<Date> signDates = new ArrayList<>();
        if (bean.getDateList() != null && bean.getDateList().size() > 0) {
            for (int i = 0; i < bean.getDateList().size(); i++) {
                Date date = new Date(bean.getDateList().get(i));
                signDates.add(date);
            }
        }
        monthData.setSignDates(signDates);
        monthDates.add(0, monthData);
        signCalendar.setSignDatas(monthDates);
        signCalendar.datePager.setCurrentItem(1);

    }

    @Override
    public void upDateUI3(SignBean bean, int mYear, int mMonth) {
        month = mMonth;
        year = mYear;
        MonthSignData monthData = new MonthSignData();
        monthData.setYear(mYear);
        monthData.setMonth(mMonth - 1);
        ArrayList<Date> signDates = new ArrayList<>();
        if (bean.getDateList() != null && bean.getDateList().size() > 0) {
            for (int i = 0; i < bean.getDateList().size(); i++) {
                Date date = new Date(bean.getDateList().get(i));
                signDates.add(date);
            }
        }
        monthData.setSignDates(signDates);
        monthDates.add(0, monthData);
        signCalendar.setSignDatas(monthDates);
        if (signCalendar.returnIndex() > 2) {
            signCalendar.datePager.setCurrentItem(0);
            LogUtils.d("+++++++++++++++++++++++");
        }
    }

    @Override
    public void upDateSign() {
        isSign = false;
        sign.setText("已签到");
        monthToday.getSignDates().add(new Date());
        if (monthDates.size() > 2) {
            monthDates.remove(monthDates.size() - 2);
            monthDates.add(monthDates.size() - 1, monthToday);
            signCalendar.setSignDatas(monthDates);
        }

    }

    @Override
    public void upDateIntegral(IntegralBean str) {
        signIntegral.setText("您当前拥有" + str.getIntegral() + "积分");
    }
}
